package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.util.Random;

import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class GroupRemovalTest extends TestBase {
	@Test
	public void deleteSomeGroup() {
		
		// save old state
		SortedListOf<GroupData> oldList = new SortedListOf<GroupData>(app
				.getHibernateHelper().listGroups());
		
		Random rnd = new Random();
		int index = rnd.nextInt(oldList.size()-1);
		
		// action
		app.getGroupHelper().deleteGroup(index);

		
		// save new state
		SortedListOf<GroupData> newList = new SortedListOf<GroupData>(app
				.getHibernateHelper().listGroups());

		// compare states
		assertThat(newList, equalTo(oldList.without(index)));
		
		if (wantToCheck()) {
			if ("yes".equals(app.getProperty("check.db"))) {
				assertThat(app.getApplicationModel().getGroups(), equalTo(app
						.getHibernateHelper().listGroups()));
			}
			if ("yes".equals(app.getProperty("check.db"))) {
				assertThat(app.getApplicationModel().getGroups(), equalTo(app
						.getGroupHelper().getUIGroups()));
			}
		}
	}

}
